#include <StructDescr.h>

Model::Model()
{
	this->setupUi(this);
	Count = 0;
}

void Model::addItem(QString &s, bool form)
{
	QTreeWidgetItem *tempChild = new QTreeWidgetItem((QTreeWidgetItem *)0, QStringList(s),0);
	if (treeWidget->currentItem())
	{
		treeWidget->currentItem()->addChild(tempChild);
	}
	else treeWidget->addTopLevelItem(tempChild);
	if(form)treeWidget->setCurrentItem(tempChild);
	else treeWidget->expandItem(treeWidget->currentItem());
}

int Model::showItemChangeDialog()
{
	if(treeWidget->currentItem()== NULL) return -1;
	AskDoubleDialog * Dialog = new AskDoubleDialog(this);
	Dialog->lineEdit->setText(treeWidget->currentItem()->text(0));
	if(Dialog->exec())
	{
		treeWidget->currentItem()->setText(0, Dialog->lineEdit->text());
		delete Dialog;
		return 1;
	}
	delete Dialog;
	return 0;
}

int Model::Delete(QTreeWidgetItem * currentItem)
{
	QList<QTreeWidgetItem*> list;
	if(!currentItem) currentItem = treeWidget->invisibleRootItem();
	else
	{
		currentItem = treeWidget->currentItem();
		if(currentItem == NULL) return 0;
		list.append(currentItem);
	}
	list += CollectItems(currentItem); //or //CollectItems(currentItem, list);
	int retVal = list.size();
	qDeleteAll(list);
	return retVal;
}

//

QString Model::CounterToCaption()
{
	Count = Count.toInt() + 1;
	return Count.toString();
}

QList<QTreeWidgetItem*> Model::CollectItems(QTreeWidgetItem * curItem)
{
	QList <QTreeWidgetItem*> lTemp = curItem->takeChildren();
	foreach(QTreeWidgetItem * temp, lTemp)
	{
		lTemp += CollectItems(temp);
	}
	return lTemp;
}

void Model::CollectItems(QTreeWidgetItem * curItem, QList<QTreeWidgetItem*> &list)
{
	foreach(QTreeWidgetItem * temp, curItem->takeChildren())
	{
		CollectItems(temp, list);
		list.append(temp);
	}
}

bool Model::saveTree(const QString &FName)
{
	QTreeWidgetItem * header = treeWidget->invisibleRootItem();
	if(header == NULL) return false;

	TiXmlDocument doc;
	TiXmlDeclaration * decl = new TiXmlDeclaration("1.0", "", "");
	doc.LinkEndChild(decl);

	TiXmlElement * root = new TiXmlElement("Root");
	pickUpChildren(header, root, QVariant(1));
	doc.LinkEndChild(root);
	if(!doc.SaveFile(FName.toStdString().c_str()))
		return false;
	return true;
}

void Model::pickUpChildren(QTreeWidgetItem * header, TiXmlElement * parent, QVariant &inp)
{
	int countItems = header->childCount();
	QTreeWidgetItem * item;
	TiXmlElement * elem;
	for(int i = 0; i < countItems; i++)
	{
		item = header->child(i);
		elem = new TiXmlElement(QString("item" + inp.toString()).toStdString().c_str());
		elem->SetAttribute("a", item->text(0).toStdString().c_str());
		pickUpChildren(item, elem, QVariant(inp.toInt()+1));
		parent->LinkEndChild(elem);
	}
}

bool Model::loadTree(QString &FileName)
{
	Delete();
	TiXmlDocument doc(FileName.toStdString().c_str());
	if(doc.LoadFile())
	{
		buildTree(treeWidget->invisibleRootItem(), &doc);
		treeWidget->setCurrentItem(treeWidget->invisibleRootItem()->child(0));
		treeWidget->expandAll();
		return true;
	}
	return false;
}

void Model::buildTree(QTreeWidgetItem * item, TiXmlNode * node)
{
	QTreeWidgetItem * temp;
	int count = 0;
	QString text;
	for(TiXmlNode* pChild = node->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
	{
		if(!pChild) return;
		switch(pChild->Type())
		{
		case TiXmlNode::DOCUMENT: 
			break;

		case TiXmlNode::COMMENT: 
			break;
		
		case TiXmlNode::DECLARATION:
			break;

		case TiXmlNode::ELEMENT:
			text = QString(dynamic_cast<TiXmlElement*>(pChild)->Attribute("a", &count));
			if(count == 0) 
			{
				buildTree(item, pChild);				
				break;
			}
			temp = new QTreeWidgetItem(static_cast<QTreeWidgetItem *>(0), QStringList(text), 0);
			item->addChild(temp);
			buildTree(temp, pChild);			
			break;

		case TiXmlNode::TEXT:
			break;

		case TiXmlNode::UNKNOWN:
			break;
		}
	}
}


bool Model::fileSaveDialog(QString &FileName)
{
	QFileDialog *dlg = new QFileDialog(this, Qt::Dialog);
	dlg->setFileMode(QFileDialog::AnyFile);
	dlg->setAcceptMode(QFileDialog::AcceptSave);
	dlg->setDefaultSuffix("xml");
	dlg->setConfirmOverwrite(true);
	dlg->setFilter(tr("XML-files (*.xml)"));
	if(dlg->exec())
	{
		FileName = dlg->selectedFiles()[0];
		delete dlg;
		return true;
	}
	delete dlg;
	return false;
}

bool Model::fileOpenDialog(QString &FileName)
{
	QFileDialog *dlg = new QFileDialog(this, Qt::Dialog);
	dlg->setFileMode(QFileDialog::ExistingFile);
	dlg->setAcceptMode(QFileDialog::AcceptOpen);
	dlg->setDefaultSuffix("xml");
	dlg->setFilter(tr("XML-files (*.xml)"));
	if(dlg->exec())
	{
		FileName = dlg->selectedFiles()[0];
		delete dlg;
		return true;
	}
	delete dlg;
	return false;	
}